package handle.data;

import save.data.User;
import utils.DataBaseUtil;
import utils.MD5Util;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


@WebServlet(name = "register", value = "/register")
public class register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Connection con = null;
        PreparedStatement sql=null;
        ResultSet resultSet;
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();
        String confirmPassword = request.getParameter("confirmPassword").trim();

        if(username==null)
            username="";
        if(password==null)
            password="";
        if(!password.equals(confirmPassword)) {
            request.setAttribute("Rmessage","两次密码不同，注册失败");
            RequestDispatcher dispatcher=
                    request.getRequestDispatcher("register.jsp");
            dispatcher.forward(request, response);//转发
            return;
        }
        boolean isLD=true;
        for(int i=0;i<username.length();i++){
            char c=username.charAt(i);
            if(!(Character.isLetterOrDigit(c)||c=='_'))
                isLD=false;
        }
        boolean boo=username.length()>0&&password.length()>0&&isLD;

        try {
            con = DataBaseUtil.getConnection();

            String query ="select * from user where username=?";
            sql = con.prepareStatement(query);
            sql.setString(1,username);
            resultSet=sql.executeQuery();


            //如果数据库中已有该名称注册的用户
            if ( resultSet.next()){
                request.setAttribute("Rmessage", "该用户名已经被注册");
                RequestDispatcher dispatcher=
                        request.getRequestDispatcher("register.jsp");
                dispatcher.forward(request, response);//转发
                return;
            }

            String insertCondition = "INSERT INTO user(username,password) VALUES (?,?)";
            sql = con.prepareStatement(insertCondition);
            if (boo) {
                sql.setString(1, username);
                password = MD5Util.encrypt(password);//给用户密码加密。
                sql.setString(2, password);
                int m = sql.executeUpdate();
                if (m != 0) {

                    response.sendRedirect("login.jsp");
                }
            } else {
                request.setAttribute("Rmessage", "信息填写不完整或名字中有非法字符");
                RequestDispatcher dispatcher=
                        request.getRequestDispatcher("register.jsp");
                dispatcher.forward(request, response);//转发
            }
        }catch (Exception e){
            System.out.println("注册是的错误是："+e);
        }
        finally {
            DataBaseUtil.close(null,null,con,sql);
        }


    }
}
